package com.xingxuecn.limit.web.action.sys;

import com.opensymphony.xwork2.ActionSupport;
import com.xingxuecn.limit.common.model.Page;
import com.xingxuecn.limit.common.model.ResultModel;
import com.xingxuecn.limit.common.model.Role;
import com.xingxuecn.limit.common.utils.ResponseUtils;
import com.xingxuecn.limit.service.RoleService;
import com.xingxuecn.limit.service.impl.RoleServiceImpl;
import org.apache.struts2.ServletActionContext;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 你的名字 at 2018/9/10 11:18
 */
public class RoleAction extends ActionSupport {

    private RoleService roleService = new RoleServiceImpl();
    private Role role =new Role();
   public void setRole(Role role){
       this.role=role;
   }
   public Role getRole(){
       return role;
   }


    private Integer page;
    private Integer rows;


    public String index(){
        return SUCCESS;
    }



    public String search() {
        HttpServletResponse response = ServletActionContext.getResponse();
        List<Role> list = null;
        try {
            list = roleService.search(role);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ResponseUtils.writeJson(list, response);
        return null;
    }
    public String searchPage() {
        HttpServletResponse response = ServletActionContext.getResponse();
        Page result = null;
        try {
            result = roleService.searchPage(role,page,rows);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ResponseUtils.writeJson(result, response);
        return null;
    }



    public String add() {
        ResultModel resultModel = null;
        try {
            roleService.add(role);
            resultModel = new ResultModel(201, "新增成功");
        } catch (SQLException e) {
            e.printStackTrace();
            resultModel = new ResultModel(500, "新增失败");
        }

        HttpServletResponse response = ServletActionContext.getResponse();
        ResponseUtils.writeJson(resultModel, response);
        return null;
    }

    public String update() {
        ResultModel resultModel = null;
        try {
            roleService.update(role);
            resultModel = new ResultModel(201, "修改成功");
        } catch (SQLException e) {
            e.printStackTrace();
            resultModel = new ResultModel(500, "修改失败");
        }

        HttpServletResponse response = ServletActionContext.getResponse();
        ResponseUtils.writeJson(resultModel, response);
        return null;
    }


    public String delete() {
        ResultModel resultModel = null;
        try {
            roleService.delete(role.getId());
            resultModel = new ResultModel(200, "删除成功");
        } catch (SQLException e) {
            e.printStackTrace();
            resultModel = new ResultModel(500, "删除失败");
        }

        HttpServletResponse response = ServletActionContext.getResponse();
        ResponseUtils.writeJson(resultModel, response);
        return null;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }


}
